package com.dtyunxi.generate;

import com.dtyunxi.domain.EntityAttr;
import com.dtyunxi.constant.TypeMapper;
import com.dtyunxi.domain.TableDto;
import com.dtyunxi.generate.base.CodeGenerateBase;
import com.dtyunxi.utils.DBUtil;
import com.dtyunxi.utils.ResourceUtil;
import com.dtyunxi.utils.StringUtil;

import javax.management.Attribute;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 生成 huieryun-dao 实体
 * <p/>
 * <br>==========================
 * <br> 公司：云徙科技
 * <br> 开发：wu.yingnan@dtyunxi.com
 * <br> 版本：1.0
 * <br> 创建时间：2017/4/11
 * <br>==========================
 */
public class EoGenerateService extends CodeGenerateBase {

    private static final DBUtil dbUtil = new DBUtil();

    private String database;

    public EoGenerateService() {
        setTemplateFile("entity.ftl");
        setSourcePackage(ResourceUtil.get("dto.package"));
    }

    public void init(TableDto table) throws Exception {
        StringBuilder sb = new StringBuilder("select COLUMN_NAME as column_name, DATA_TYPE as data_type, COLUMN_COMMENT as column_comment from `columns` where `table_schema`='").append(getDatabase()).append("' and `table_name`='").append(table.getTableName()).append("';");
        System.out.println(sb.toString());
        PreparedStatement pstate = dbUtil.getConnection().prepareStatement(sb.toString());
        ResultSet results = pstate.executeQuery();

        String className = StringUtil.getCamelString(table.getTableName());
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("packageName", getSourcePackage());
//        root.put("className", className.substring(2));
        root.put("className", className);
        root.put("tableComment", table.getTableComment());
//        root.put("table", table.getTableName());
        root.put("tableName", table.getTableName());
        root.put("author", ResourceUtil.get("author"));
        root.put("date", new SimpleDateFormat("yyyy/MM/dd").format(new Date()));

        List<Attribute> attrList = new ArrayList<Attribute>();
        while (results.next()) {
            String fieldName = results.getString("column_name");
            List<String> ignores = Arrays.asList("id", "create_time", "create_user", "modify_time", "modify_user");
            if (ignores.contains(fieldName)) {
                continue;
            }
//            if ("create_time".equals(fieldName) || "created_by".equals(fieldName) || "update_time".equals(fieldName) || "updated_by".equals(fieldName)) {
//                continue;
//            }
            String type = TypeMapper.mapper.get(results.getString("data_type"));
            attrList.add(new EntityAttr(StringUtil.underlineToCamel(fieldName), fieldName, type, results.getString("column_comment")));
        }

        root.put("attrs", attrList);
        setAttr(root);
//        setSourceFile(new StringBuilder(className).append("Eo.java").toString().substring(2));
        setSourceFile(new StringBuilder(className).append("Eo.java").toString());
    }

    public List<TableDto> showTables() throws Exception{
        String sql = new StringBuilder("select table_name,table_comment from `TABLES` where `table_schema`='").append(getDatabase()).append("';").toString();
        System.out.println(sql);
        PreparedStatement pstate = dbUtil.getConnection().prepareStatement(sql);
        ResultSet results = pstate.executeQuery();
        List<TableDto> tables = new ArrayList<TableDto>();

        while (results.next()){
            TableDto dto = new TableDto();
            String tableName = results.getString("table_name");
            String tableComment = results.getString("table_comment");

            dto.setTableName(tableName);
            dto.setTableComment(tableComment);

            tables.add(dto);
        }

        return tables;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

}
